package edu.ccut.saturn.component.utils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import edu.ccut.saturn.component.ILogger;
import edu.ccut.saturn.component.SaturnData;
import edu.ccut.saturn.component.SaturnLoggerManager;

/**
 * validate a classType Type. support full name, simple name and base name
 * 
 * @author LeslieGu
 * 
 */
public abstract class TypeValidation {

	private static final String LOGGER_KEY_BUNDLE_RUNTIME = SaturnLoggerManager.LOGGER_KEY_COMPONENT_RUNTIME;

	protected static ILogger logger = SaturnLoggerManager.getInstance()
			.getSaturnLogger(LOGGER_KEY_BUNDLE_RUNTIME);

	public static boolean isInteger(String classType) {
		return Integer.class.getName().equals(classType)
				|| Integer.class.getSimpleName().equals(classType)
				|| "int".equals(classType);
	}

	public static boolean isDecimal(String classType) {
		return BigDecimal.class.getName().equals(classType)
				|| BigDecimal.class.getSimpleName().equals(classType)
				|| "decimal".equals(classType);
	}

	public static boolean isDouble(String classType) {
		return Double.class.getName().equals(classType)
				|| Double.class.getSimpleName().equals(classType)
				|| "double".equals(classType);
	}

	public static boolean isFloat(String classType) {
		return Float.class.getName().equals(classType)
				|| Float.class.getSimpleName().equals(classType)
				|| "float".equals(classType);
	}

	public static boolean isBoolean(String classType) {
		return Boolean.class.getName().equals(classType)
				|| Boolean.class.getSimpleName().equals(classType)
				|| "boolean".equals(classType);
	}

	public static boolean isCharacter(String classType) {
		return Character.class.getName().equals(classType)
				|| Character.class.getSimpleName().equals(classType)
				|| "char".equals(classType);
	}

	public static boolean isByte(String classType) {
		return Byte.class.getName().equals(classType)
				|| Byte.class.getSimpleName().equals(classType)
				|| "byte".equals(classType);
	}

	public static boolean isShort(String classType) {
		return Short.class.getName().equals(classType)
				|| Short.class.getSimpleName().equals(classType)
				|| "short".equals(classType);
	}

	public static boolean isLong(String classType) {
		return Long.class.getName().equals(classType)
				|| Long.class.getSimpleName().equals(classType)
				|| "long".equals(classType);
	}

	public static boolean isList(String classType) {
		return List.class.getName().equals(classType)
				|| List.class.getSimpleName().equals(classType)
				|| "list".equals(classType)
				|| ArrayList.class.getName().equals(classType)
				|| ArrayList.class.getSimpleName().equals(classType)
				|| "arrayList".equals(classType)
				|| LinkedList.class.getName().equals(classType)
				|| LinkedList.class.getSimpleName().equals(classType)
				|| "linkedList".equals(classType);
	}

	public static boolean isSaturnData(String classType) {
		return SaturnData.class.getName().equals(classType)
				|| SaturnData.class.getSimpleName().equals(classType)
				|| "saturnData".equals(classType);
	}

	public static boolean isString(String classType) {
		return String.class.getName().equals(classType)
				|| String.class.getSimpleName().equals(classType)
				|| "string".equals(classType);
	}

	/**
	 * value会纠错处理为classtype
	 * 
	 * @param classType
	 * @param value
	 * @return
	 */
	public static Object getValidTypeValue(String classType, Object value)
			throws NumberFormatException {

		if (value == null) {
			return null;
		}

		if (classType == null || classType.trim().equals("")) {
			return value;
		}

		if (isString(classType)) {
			return "" + value;

		} else if (isInteger(classType)) {
			return Integer.parseInt(value.toString());

		} else if (isBoolean(classType)) {
			return Boolean.parseBoolean(value.toString());

		} else if (isLong(classType)) {
			return Long.parseLong(value.toString());

		} else if (isList(classType)) {
			return (List<?>) value;

		} else if (isDouble(classType)) {
			return Double.parseDouble(value.toString());

		} else if (isFloat(classType)) {
			return Float.parseFloat(value.toString());

		} else if (isCharacter(classType)) {
			return value.toString().trim().charAt(0);

		} else if (isByte(classType)) {
			return Byte.parseByte(value.toString());

		} else if (isShort(classType)) {
			return Short.parseShort(value.toString());

		} else if (isDecimal(classType)) {
			return new BigDecimal(value.toString());
		}

		return value;
	}

}
